package collection.stream;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

public class StreamApiDemo2 {
    public static void main(String[] args) {
        List<Integer> list = new ArrayList<>();
        Random r = new Random();
        for (int i = 0; i < 10; i++) {
            list.add(r.nextInt(100));
        }
        System.out.println(list);

        List<Integer> list1 = list.stream().filter(x -> x % 2 == 0).collect(Collectors.toList());
        //仅保留满足lambda表达式要求的元素
        System.out.println(list1);

        List<Integer> list2 = list.stream().distinct().collect(Collectors.toList());
        //去重
        System.out.println(list2);

        List<Integer> list3 = list.stream().sorted((x1, x2) -> x2 - x1).collect(Collectors.toList());
        //排序，倒序
        System.out.println(list3);

        List<Integer> list4 = list.stream()
                .map(x -> x * 5)
                .filter(x -> x % 2 == 0)
                .distinct()
                .sorted((x1, x2) -> x2 - x1)
                .collect(Collectors.toList());
        System.out.println(list4);

    }
}
